/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sui.bigdata.flink.sql.sink.jdbc.table;

import com.sui.bigdata.flink.sql.core.table.AbsTableParser;
import com.sui.bigdata.flink.sql.core.table.TableInfo;
import com.sui.bigdata.flink.sql.core.util.MathUtil;

import java.util.Map;

/**
 * Reason:
 * Date: 2019/9/23
 * @author yong_chen@sui.com
 */
public class JdbcSinkParser extends AbsTableParser {
    @Override
    public TableInfo getTableInfo(String tableName, String fieldsInfo, Map<String, Object> props) {
        JdbcTableInfo jdbcTableInfo = new JdbcTableInfo();
        jdbcTableInfo.setName(tableName);
        parseFieldsInfo(fieldsInfo, jdbcTableInfo);

        jdbcTableInfo.setParallelism(props.get(JdbcTableInfo.PARALLELISM_KEY.toLowerCase()));
        jdbcTableInfo.setUrl(MathUtil.getString(props.get(JdbcTableInfo.URL_KEY.toLowerCase())));
        jdbcTableInfo.setTableName(MathUtil.getString(props.get(JdbcTableInfo.TABLE_NAME_KEY.toLowerCase())));
        jdbcTableInfo.setUserName(MathUtil.getString(props.get(JdbcTableInfo.USER_NAME_KEY.toLowerCase())));
        jdbcTableInfo.setPassword(MathUtil.getString(props.get(JdbcTableInfo.PASSWORD_KEY.toLowerCase())));
        jdbcTableInfo.setBatchSize(MathUtil.getIntegerVal(props.get(JdbcTableInfo.BATCH_SIZE_KEY.toLowerCase())));
        jdbcTableInfo.setBatchWaitInterval(MathUtil.getLongVal(props.get(JdbcTableInfo.BATCH_WAIT_INTERVAL_KEY.toLowerCase())));
        jdbcTableInfo.setBufferSize(MathUtil.getString(props.get(JdbcTableInfo.BUFFER_SIZE_KEY.toLowerCase())));
        jdbcTableInfo.setFlushIntervalMs(MathUtil.getString(props.get(JdbcTableInfo.FLUSH_INTERVALMS_KEY.toLowerCase())));
        jdbcTableInfo.setOperation(MathUtil.getString(props.get(JdbcTableInfo.OPERATION.toLowerCase())));
        jdbcTableInfo.setBootstrapServers(MathUtil.getString(props.get(JdbcTableInfo.BOOTSTRAP_SERVERS.toLowerCase())));
        jdbcTableInfo.setTopic(MathUtil.getString(props.get(JdbcTableInfo.TOPIC.toLowerCase())));

        jdbcTableInfo.check();
        return jdbcTableInfo;
    }
}
